import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;

public class Dictionary {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String temp = "";
		String fileInput = "dictionary.dd";
		// System.out.println(readFile2(fileName));

		String fileOutput = "dictionary.txt";
		// FileWriter fw = new FileWriter(f, true);
		Writer writer = new OutputStreamWriter(
				new FileOutputStream(fileOutput), "UTF-8");

		BufferedWriter bw = new BufferedWriter(writer);

		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(fileInput), "UTF8"));
		try {
			String str;
			while ((str = br.readLine()) != null) {
				if(str.contains("#")){
					str = str.substring(0, str.indexOf("#"));
					if(str.contains(" ")){
						str = str.substring(0, str.indexOf(" "));
						temp += str.trim() + " ";
//						bw.write(str);
//						bw.newLine();				
					} else {
						temp += str.trim() + " ";
//						bw.write(str);
//						bw.newLine();	
					}
				}				
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
//		bw.close();
		System.out.println(temp);
		
		String[] data = temp.split(" ");

		LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<String, Integer>();

		for (String key : data)
		{
			Integer freq = linkedHashMap.get(key);
			if(freq == null) freq = 1; else freq ++;
			linkedHashMap.put(key, freq);
		}
		System.out.println(linkedHashMap);
		
		for (Entry<String, Integer> entry : linkedHashMap.entrySet()) {
			bw.write(entry.getKey());
			bw.newLine();	
        }
		bw.close();
	}

	static String readFile(String fileName) {
		String temp = "";
		try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
			String sCurrentLine;
			while ((sCurrentLine = br.readLine()) != null) {
				temp += sCurrentLine;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return temp;
	}

	static String readFile2(String fileName) {
		String temp = "";
		try {
			File fileDir = new File(fileName);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileDir), "UTF8"));

			String str;

			while ((str = in.readLine()) != null) {
				temp += str;
			}

			in.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return temp;
	}

	static void writeFile(String fileName, String writeString) {
		try {
			File fileDir = new File(fileName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileDir), "UTF8"));

			out.append(writeString);

			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	static String filterLongWord(String article) {
		String temp = "";
		String[] tempArray;
		tempArray = article.split(" ");

		for (int i = 0; i < tempArray.length; i++) {
			if (tempArray[i].length() < 8) {
				temp += tempArray[i] + " ";
			}
		}

		return temp;
	}

	static ArrayList<Article> sortArrayList(ArrayList<Article> arrayList) {
		for (int i = 0; i < arrayList.size() - 1; i++) {
			Locale locales[] = Locale.getAvailableLocales();
			System.out.println(locales);

			int min = i;
			for (int j = i + 1; j < arrayList.size(); j++) {
				if (arrayList.get(i).getWord()
						.compareTo(arrayList.get(j).getWord()) > 0) {
					min = j;
				}
			}
			if (min != i) {
				Article tempArticle = new Article();
				tempArticle.setWord(arrayList.get(i).getWord());
				tempArticle.setNumOfWord(arrayList.get(i).getNumOfWord());

				arrayList.get(i).setWord(arrayList.get(min).getWord());
				arrayList.get(i)
						.setNumOfWord(arrayList.get(min).getNumOfWord());

				arrayList.get(min).setWord(tempArticle.getWord());
				arrayList.get(min).setNumOfWord(tempArticle.getNumOfWord());
			}
		}

		return arrayList;
	}
}
